﻿namespace TextKeep;

public class FileUtil
{
    public static string GetNextFolderName(string rootPath, string folderPrefix)
    {
        int index = 1;
        string newFolderName = $"{folderPrefix}{index}";

        while (Directory.Exists(Path.Combine(rootPath, newFolderName)))
        {
            index++;
            newFolderName = $"{folderPrefix}{index}";
        }

        return newFolderName;
    }

    public static string GetNextFileName(string rootPath, string filePrefix)
    {
        int index = 1;
        string newFileName = $"{filePrefix}{index}";

        while (File.Exists(Path.Combine(rootPath, newFileName)))
        {
            index++;
            newFileName = $"{filePrefix}{index}";
        }

        return newFileName;
    }


    // 读取文本文件
    public static string ReadTextFile(string filePath)
    {
        try
        {
            string fileContent = File.ReadAllText(filePath);
            return fileContent;
        }
        catch (Exception ex)
        {
            Console.WriteLine($"An error occurred while reading the file: {ex.Message}");
            return null;
        }
    }

    // 写入文本文件
    public static void WriteTextFile(string filePath, string content)
    {
        try
        {
            File.WriteAllText(filePath, content);
        }
        catch (Exception ex)
        {
            Console.WriteLine($"An error occurred while writing to the file: {ex.Message}");
        }
    }
}